﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MoleculeViewer.Entities;
using System.IO;
using System.Xml.Linq;
using System.Windows.Browser;

namespace MoleculeViewer.DAO
{
    public class MoleculeDAO
    {
        public event EventHandler<GetMoleculeEventArgs> GetMoleculeCompleted;

        public void GetMolecule(string moleculename)
        {
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            var appUrl = HtmlPage.Document.DocumentUri.AbsolutePath.Substring(0, HtmlPage.Document.DocumentUri.AbsolutePath.LastIndexOf("/"));
            client.OpenReadAsync(new Uri(appUrl + "/Xml/" + moleculename + ".xml", UriKind.Relative));
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
                HtmlPage.Window.Alert("Error the molecule");
            else
            {
                using (Stream s = e.Result)
                {
                    var molecule = (from m in XDocument.Load(s).Descendants("molecule")
                     select new Molecule()
                     {
                         Atoms = (from a in m.Descendants("atomArray").First().Descendants("atom")
                                  select new Atom()
                                  {
                                      Id = a.Attribute("id").Value,
                                      ElementType = a.Elements("string").Single(element => element.Attribute("builtin").Value == "elementType").Value
                                  }).ToList(),
                         Bonds = (from b in m.Descendants("bondArray").First().Descendants("bond")
                                  select new Bond()
                                  {
                                      IdFrom = b.Elements("string").Where(element => element.Attribute("builtin").Value == "atomRef").First().Value,
                                      IdTo = b.Elements("string").Where(element => element.Attribute("builtin").Value == "atomRef").ElementAt(1).Value,
                                      Order = int.Parse(b.Elements("string").Where(element => element.Attribute("builtin").Value == "order").First().Value),
                                  }).ToList()
                     }).First();

                    if (GetMoleculeCompleted != null)
                        GetMoleculeCompleted(this, new GetMoleculeEventArgs()
                        {
                            Molecule = molecule
                        });
                }
            }
        }
    }

    public class GetMoleculeEventArgs : EventArgs
    {
        public Molecule Molecule
        {
            get;
            set;
        }
    }

}
